package com.sxbbc.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.CustomerMapping;
import com.sxbbc.api.service.IBaseService;
import com.sxbbc.api.service.ICustomerService;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.regular.RpxUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.basics.utils.token.TokenUtils;
import com.sxbbc.common.core.dto.jz.CustomerTeamVo;
import com.sxbbc.common.core.dto.jz.CustomerVo;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.BusinessTypeEnums;
import com.sxbbc.common.core.enums.ProductStatusEnums;
import com.sxbbc.common.core.enums.ProductTypeEnums;
import com.sxbbc.common.core.service.*;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户/用户相关 控制层
 *
 * @author leehao
 * @date 2019-05-15 17:31
 */
@SuppressWarnings("all")
@RestController

public class CustomerController {

    private static final int PRODUCT_FLAG = 1;
    private static final int BUSINESS_FLAG = 2;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ICReceivedAddrService receivedAddrService;
    @Autowired
    private LoginTokenService loginTokenService;
    @Autowired
    private IBaseService baseService;
    @Autowired
    private ICFootprintInfoService footprintInfoService;
    @Autowired
    private ICCollectProService collectProService;
    @Autowired
    private ICCollectBusService collectBusService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IProcMainService procMainService;
    @Autowired
    private IOpinionInfosService opinionInfosService;
    @Autowired
    private ICCustomerService iCustomerService;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private BBusinessService bBusinessService;
    @Autowired
    private ICPushLogService cPushLogService;
    @Autowired
    private ICCustomerService cCustomerService;
    @Autowired
    private ICCustomerAuthenService customerAuthenService;
    @Autowired
    private ISysDealDataService sysDealDataService;

    //region 用户收货地址
    /**
     * 获取用户收货地址列表
     * @param token 获取cid
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = CustomerMapping.SHOW_CUSTOMER_RECEIVED_ADDR_LIST)
    public RestResponse showCustomerReceivedAddrList(String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        QueryWrapper<CReceivedAddr> ew = new QueryWrapper<>();
        ew.eq("cid",cid);
        List<CReceivedAddr> addrList = receivedAddrService.list(ew);
        addrList.forEach(x -> x.setCid(null));
        return GetRest.getSuccess("success",addrList);
    }

    /**
     * 修改用户收货地址 回显
     * @param id 地址id
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = CustomerMapping.SHOW_EDIT_CUSTOMER_RECEIVED_ADDR)
    public RestResponse showEditCustomerReceiverAddr(Integer id) {
        CReceivedAddr cReceivedAddr = receivedAddrService.getById(id);
        cReceivedAddr.setCid(null);
        return GetRest.getSuccess("success",cReceivedAddr);
    }

    /**
     * 新增/编辑用户收货地址
     * @param receivedAddr 收货地址
     * @param token 获取cid
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = CustomerMapping.EDIT_CUSTOMER_RECEIVED_ADDR)
    public RestResponse editCustomerReceivedAddr(CReceivedAddr receivedAddr, String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        if (StringUtils.isBlank(receivedAddr.getName())) {
            return GetRest.getFail("请输入收货人姓名");
        }
        if (StringUtils.isBlank(receivedAddr.getPhone())) {
            return GetRest.getFail("请输入收货人电话");
        }
        if (!StringUtils.equals(RpxUtils.getInstance().validPhone(receivedAddr.getPhone()),"")) {
            return GetRest.getFail(RpxUtils.getInstance().validPhone(receivedAddr.getPhone()));
        }
        if (receivedAddr.getProId() == null || receivedAddr.getCityId() == null) {
            return GetRest.getFail("请选择收货地址");
        }
        if (StringUtils.isBlank(receivedAddr.getAddress())) {
            return GetRest.getFail("请输入收货详细地址");
        }
        //查询是否有地址 没有该条设置默认
        QueryWrapper<CReceivedAddr> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid);
        int addrCount = receivedAddrService.count(wrapper);
        if (addrCount < 1) {
            receivedAddr.setDefaul(StaticUtils.STATUS_YES);
        }else{
            //查询是否有默认的地址
            if (CalcuUtils.getInstance().isEquals(receivedAddr.getDefaul(),StaticUtils.STATUS_YES)) {
                wrapper.eq("defaul",StaticUtils.STATUS_YES);
                CReceivedAddr cReceivedAddr = receivedAddrService.getOne(wrapper);
                if (cReceivedAddr != null) {
                    cReceivedAddr.setDefaul(StaticUtils.STATUS_NO);
                    receivedAddrService.updateById(cReceivedAddr);
                }
            }
        }
        // 拼接的地址
        String addrName = addressService.getAddrName(receivedAddr.getProId(), receivedAddr.getCityId(), receivedAddr.getAreaId());
        receivedAddr.setNameExt(addrName);
        receivedAddr.setCid(cid);
        receivedAddr.setCreatetime(new Date());
        boolean save = receivedAddrService.saveOrUpdate(receivedAddr);
        if (!save) {
            throw new ThrowJsonException("保存用户收货地址失败");
        }
        return GetRest.getSuccess("success");
    }

    /**
     * 删除用户收货地址
     * @param id 地址id
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = CustomerMapping.DELETE_CUSTOMER_RECEIVED_ADDR)
    public RestResponse deleteCustomerReceivedAddr(String token,Integer id) {
        Integer cid = loginTokenService.validLoginCid(token);
        CCustomer cCustomer = cCustomerService.getById(cid);
        if (cCustomer == null) {
            return GetRest.getFail("该用户不存在");
        }
        boolean del = receivedAddrService.removeById(id);
        if (!del) {
            throw new ThrowJsonException("删除用户收货地址失败");
        }
        return GetRest.getSuccess("success");
    }

    /**
     * 处理用户设置收货地址为默认
     * @param addr 关键字段
     *             id 地址id
     *             defaul 是否默认
     * @param token 获取cid
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = CustomerMapping.HANDLE_CUSTOMER_RECEIVED_DEFAULT)
    public RestResponse handleReceivedAddrDefault(CReceivedAddr addr,String token){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        addr.setCid(cid);
        if (CalcuUtils.getInstance().isEquals(addr.getDefaul(), StaticUtils.STATUS_YES)) {
            CReceivedAddr updateAddr = new CReceivedAddr();
        	updateAddr.setDefaul(StaticUtils.STATUS_NO);
        	QueryWrapper<CReceivedAddr> updateWrapper = new QueryWrapper<CReceivedAddr>();
        	updateWrapper.eq("cid", cid);
        	receivedAddrService.update(updateAddr, updateWrapper);
        }
        boolean update = receivedAddrService.updateById(addr);
        if (!update) {
            throw new ThrowJsonException("设置默认地址失败");
        }
        return GetRest.getSuccess("success");
    }


    //endregion

    //region 用户足迹
    /**
     * 用户浏览记录
     * @param token 获取cid
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = CustomerMapping.SHOW_CUSTOMER_FOOT_PRINT)
    public RestResponse showCustomerFootPrint(String token, BasePage<CFootprintInfo> page){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        CFootprintInfo foot = new CFootprintInfo();
        foot.setCid(cid);
        IPage<CFootprintInfo> pageInfo = footprintInfoService.queryCustomerFootList(foot,page);
        pageInfo.getRecords().forEach(x -> x.setCid(null));
        return GetRest.getSuccess("success",pageInfo);
    }

    /**
     * 删除浏览记录
     * @param token 获取cid
     * @param id 浏览记录id
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = CustomerMapping.DELETE_CUSTOMER_FOOT_PRINT)
    public RestResponse deleteFootPrint(String token,Integer id){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        //单删
        boolean del = footprintInfoService.removeById(id);

        //全删
        if(id == null){
            QueryWrapper<CFootprintInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("cid",cid);
            del = footprintInfoService.remove(wrapper);
        }
        if (!del) {
            throw new ThrowJsonException("删除用户浏览记录失败");
        }
        return GetRest.getSuccess("success");
    }

    //endregion

    //region 用户收藏
    /**
     * 收藏商品列表
     * @param page 分页
     * @param token 获取cid
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = CustomerMapping.SHOW_COLLECT_PRODUCT)
    public RestResponse showCustomerCollectProduct(BasePage<CCollectPro> page,String token){
        Integer cid = loginTokenService.validLoginCid(token);
        if (cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        QueryWrapper<CCollectPro> wrapper = new QueryWrapper<CCollectPro>();
        wrapper.eq("cid",cid).eq("status",StaticUtils.STATUS_YES);
        //商品list
        IPage<CCollectPro> pageInfo = collectProService.page(page, wrapper);
        pageInfo.getRecords().forEach(x -> x.setCid(null));
        return GetRest.getSuccess("",pageInfo);
    }

    /**
     * 收藏商家列表
     * @param page 分页
     * @param token 获取cid
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = CustomerMapping.SHOW_COLLECT_BUSINESS)
    public RestResponse showCustomerCollectBusiness(BasePage<CCollectBus> page,String token){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        QueryWrapper<CCollectBus> wrapper = new QueryWrapper();
        wrapper.eq("cid",cid).eq("status",StaticUtils.STATUS_YES);
        IPage<CCollectBus> pageInfo = collectBusService.page(page, wrapper);
        //设置店铺的被收藏数与上架的商品总数
        for (CCollectBus b : pageInfo.getRecords()) {
            b.setCid(null);
            b.getBid();
            QueryWrapper query = new QueryWrapper<>();
            query.eq("bid",b.getBid());
            query.eq("status",StaticUtils.STATUS_YES);
            b.setCollectNum(collectBusService.count(query));
            query.eq("status", ProductStatusEnums.SHELVES.getId());
            query.eq("pro_type", ProductTypeEnums.NORMAL.getId());
            b.setProductNum(procMainService.count(query));
            BBusiness business = bBusinessService.getById(b.getBid());
            b.setbTypeName(BusinessTypeEnums.getName(business.getBType()));
            b.setBurl(business.getLogo());
            b.setBusinessName(business.getNickNameOn());
        }
        return GetRest.getSuccess("",pageInfo);
    }

    /**
     * 取消/删除收藏
     * @param token 获取cid
     * @param type 1商品/2店铺
     * @param id 对应记录的id
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = CustomerMapping.DELETE_CUSTOMER_COLLECT)
    public RestResponse deleteCustomerCollect(String token,Integer type,Integer id){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        boolean del = false;
        switch (type){
            case PRODUCT_FLAG:
                del = collectProService.removeById(id);
                break;
            case BUSINESS_FLAG:
                del = collectBusService.removeById(id);
                break;
            default:
                break;
        }
        if (!del) {
            throw new ThrowJsonException("用户取消收藏操作失败");
        }
        return GetRest.getSuccess("操作成功");
    }

    //endregion

    //region 基础功能

    /**
     * 发送修改密码验证码
     * @param token
     * @param phone
     * @return
     */
    @RequestMapping(value=CustomerMapping.SEND_EDITPWD_CODE)
    @ResponseBody
    public RestResponse sendUpdatePwdCode(String token, String phone) {
    	 Integer cid = loginTokenService.validLoginCid(token);
         if (StringUtils.isBlank(token) || cid == null) {
             return GetRest.getFail("获取用户信息失败");
         }
         return customerService.sendUpdatePwdCode(phone);
    }

    /**
     * 修改登录密码
     * @param token
     * @param phone
     * @param validcode
     * @param password
     * @param confirmPassword
     * @return
     */
    @RequestMapping(value=CustomerMapping.EDIT_PASSWORD)
    @ResponseBody
    public RestResponse handleCustomerPassword(String token,String phone,String validcode, String password) {
    	Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return customerService.handleCustomerPassword(cid,phone,validcode,password);
   }

    /**
	 * 修改用户支付密码
	 * @return
	 */
    @RequestMapping(value=CustomerMapping.EDIT_PAYWORD)
    @ResponseBody
	public RestResponse handleCustomerPayword(String phone, String validcode, String payword,Integer type) {
		Integer cid = loginTokenService.validLoginCid(TokenUtils.getToken(request));
        return customerService.handleCustomerPayword(cid,phone,validcode,payword,type);
	}

	/**
	 * 发送绑定验证码
	 * @param bindStr 绑定的账号 （根据type）
	 * @param type 1手机号   2邮箱
	 * @return
	 */
    @RequestMapping(value=CustomerMapping.SEND_BIND_CODE)
    @ResponseBody
	public RestResponse sendBindCode(String token,String bindStr,Integer type){
		Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return customerService.sendBindCode(bindStr,type);
	}

	/**
	 * 设置用户邮箱
	 * @param cid
	 * @param email
	 * @return
	 */
    @RequestMapping(value=CustomerMapping.EDIT_EMAIL)
    @ResponseBody
	public RestResponse editCustomerEmail(String token,String email,String validcode) {
		Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return customerService.updateCustomerEmail(cid,email,validcode);
	}

	/**
	 * 设置手机号
	 * @param cid
	 * @param phone
	 * @param validcode
	 * @return
	 */
    @RequestMapping(value=CustomerMapping.EDIT_PHONE)
    @ResponseBody
	public RestResponse handleCustomerPhone(String token,String phone,String validcode) {
		Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return customerService.updateCustomerPhone(cid,phone,validcode);
	}

    /**
     * 修改手机号
     * @param cid
     * @param phone
     * @param validcode
     * @return
     */
    @RequestMapping(value=CustomerMapping.UPDATE_PHONE)
    @ResponseBody
    public RestResponse updateCustomerPhone(String token,String oldphone,String newphone,String validcode) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return customerService.updateNewCustomerPhone(cid,oldphone,newphone,validcode);
    }

    /**
     * 修改会员个人资料
     * @param token 获取
     * @param headUrl
     * @param nickName
     * @return
     */
    @RequestMapping(value = CustomerMapping.EDIT_CUSTOMER_INFO)
    public RestResponse editCustomerInfo (String token, String headUrl, String nickName) {
    	Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return customerService.editCustomerInfo(cid,headUrl,nickName);
    }

    /**
     * 获取个人中心数据
     * @param token 获取cid
     * @return
     */
    @RequestMapping(value = CustomerMapping.GET_MY_INFO)
    public RestResponse getMyInfo (String token) {
    	Integer cid = loginTokenService.validLoginCid(token);
    	if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
    	CustomerVo customerVo = customerService.getMyInfo(token);
    	return GetRest.getSuccess("", customerVo);
    }

    /**
     * 通过邀请码查询推荐人相关信息
     * @param inviteCode 邀请码
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.GET_CUSTOMER_BY_CODE)
    public RestResponse getCustomerByCode(String inviteCode) {
        Map<String, Object> map = new HashMap<String, Object>();
        CCustomer queryCustomer = new CCustomer();
        queryCustomer.setInviteCode(inviteCode);
        CCustomer customer = cCustomerService.getCustomer(queryCustomer);
        if (customer != null) {
            map.put("headurl", customer.getHeadurl());
            map.put("nickName", customer.getNickName());
            map.put("qrUrl", customer.getQrApp());
        }
        return GetRest.getSuccess("", map);
    }

    /**
     * 获取微信，QQ绑定状态
     * @param token 获取cid
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.GET_ACCOUNT_BIND_STATUS)
    public RestResponse getAccountBindStatus (String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        CCustomer customer = iCustomerService.getById(cid);
        if (customer == null) {
            throw new ThrowJsonException("获取用户信息失败");
        }
        Boolean bindWx = false;
        Boolean bindQq = false;
        if (!StringUtils.isEmpty(customer.getUnionid())) {
            bindWx = true;
        }
        if (!StringUtils.isEmpty(customer.getOpenidQq())) {
            bindQq = true;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bindWx", bindWx);
        map.put("bindQq", bindQq);
        return GetRest.getSuccess("", map);
    }

    /**
     * 取消第三方绑定
     * @param token 获取cid
     * @param type 1 qq  2 wx
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.CANCEL_BIND_THIRD)
    public RestResponse cancelBindThird (String token, Integer type) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        CCustomer customer = iCustomerService.getById(cid);
        if (customer == null) {
            throw new ThrowJsonException("获取用户信息失败");
        }
        CalcuUtils calcuUtils = CalcuUtils.getInstance();
        if (calcuUtils.isEquals(type, StaticUtils.THIRD_QQ)) {
            // 第三方登录——QQ
            customer.setOpenidQq("");
        } else if (calcuUtils.isEquals(type, StaticUtils.THIRD_WX)) {
            // 第三方登录——WX
            customer.setUnionid("");
        } else {
            return GetRest.getFail("解绑失败");
        }
        boolean update = iCustomerService.updateById(customer);
        if (!update) {
            return GetRest.getFail("解绑失败");
        }
        return GetRest.getSuccess("解绑成功");
    }

    /**
     * 大陆认证需要验证码验证是否本人操作
     * @param token
     * @param reservedPhone
     * @return
     */
    @RequestMapping(value=CustomerMapping.SENDAUTHENCODE)
    @ResponseBody
    public RestResponse sendAuthenCode(String token, String reservedPhone) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return customerService.sendAuthenCode(cid,reservedPhone);
    }

    /**
     * 处理意见反馈
     * @param token 获取cid
     * @return 处理结果
     */
    @RequestMapping(value = CustomerMapping.HANDLE_OPINION)
    public RestResponse handleOpinion (String token, OpinionInfo opinion) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        if (StringUtils.isEmpty(opinion.getContent())) {
            return GetRest.getFail("请留下您的吐槽或建议");
        }
        opinion.setCid(cid);
        boolean result = opinionInfosService.save(opinion);
        if (result) {
            return GetRest.getSuccess("提交成功");
        }
        return GetRest.getFail("提交失败");
    }


    //endregion

    //region 我的团队

    /**
     * 统计团队人数
     * @param token 获取cid
     * @return Map封装数据
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.COUNT_TEAM_NUM)
    public RestResponse countTeamNum (String token) {
    	Integer cid = loginTokenService.validLoginCid(token);
    	if (StringUtils.isBlank(token) || cid == null) {
    		return GetRest.getFail("获取用户信息失败");
    	}
    	Map<String, Object> map = customerService.countTeamNum(cid);
    	return GetRest.getSuccess("", map);
    }

    /**
     * 查询我的团队 # nowLayer  直推 1 间推 2  (两者不用同时传)  levelid 1 普通  2 小型养殖户 3 大型养殖户 (两者不用同时传)
     * @param page 分页
     * @param token 获取cid
     * @return 团队分页数据
     */
    @RequestMapping(value = CustomerMapping.QUERY_MY_TEAM)
    public RestResponse queryMyTeam (BasePage<CustomerTeamVo> page, String token, CustomerTeamVo team) {
    	Integer cid = loginTokenService.validLoginCid(token);
    	if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
    	team.setCid(cid);
    	IPage<CustomerTeamVo> pageInfo = customerService.queryMyTeam(page, team);
    	return GetRest.getSuccess("", pageInfo);
    }


    //endregion

    //region 用户认证

    /**
     * 处理认证中心认证申请
     * @param token
     * @param realName 真实姓名
     * @param idcard 身份证号
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.HANDLE_CUSTOMER_AUTHEN)
    public RestResponse handleCustomerAuthen (String token, CCustomerAuthen authen) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        CCustomer customer = cCustomerService.getById(cid);
        authen.setCid(cid);
        authen.setCPhone(customer.getPhone());
        authen.setLinkPhone(RpxUtils.getInstance().validPhone(customer.getPhone()));
        return customerService.handleCustomerAuthen(authen);
    }

    /**
     * 查询用户认证数据
     * @param token 获取cid
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.QUERY_AUTHEN_DATA)
    public RestResponse queryAuthenData () {
    	Integer cid = loginTokenService.validLoginCid(TokenUtils.getToken(request));
    	QueryWrapper<CCustomerAuthen> queryWrapper = new QueryWrapper<CCustomerAuthen>();
		queryWrapper.eq("cid", cid);
		CCustomerAuthen authen = customerAuthenService.getOne(queryWrapper);
		if(authen == null){
            return GetRest.getFail("认证信息不存在");
        }
		String nameExt = addressService.getAddrName(authen.getProId(), authen.getCityId(), authen.getAreaId());
		authen.setNameExt(nameExt);
		authen.setCid(null);
    	return GetRest.getSuccess("", authen);
    }

    /**
     * 查询个人中心的平台数据
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = CustomerMapping.SHOW_PLAT_DEAL_DATA)
    public RestResponse queryPlatDealData(){
        String today = DateUtil.getInstance().nowDate(DateUtil.getInstance().DEFAULT_DATE_PATTERN);
        QueryWrapper<SysDealData> wrapper = new QueryWrapper<>();
        wrapper.last("where id = (select max(id) from sys_deal_data where status = 1)");
        SysDealData dealData = sysDealDataService.getOne(wrapper);
        if (dealData != null) {
            dealData.setTotalCal(null);
            return GetRest.getSuccess("",dealData);
        }else{
            return GetRest.getSuccess("",new SysDealData());
        }
    }


    //endregion
}
